<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\FundPaid;
use App\Admin\Actions\Grid\FundTool;
use App\Admin\Repositories\Fund;
use App\Admin\Repositories\Send;
use App\Models\User;
use Dcat\Admin\Admin;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class FundController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(
            new Fund(),
            function (Grid $grid) {
                $grid->model()->orderBy("id", "desc");

                $grid->id->sortable();
                $grid->pay_at('是否支付')->display(
                    function ($value) {
                        return $value ? "已支付" : "<span style='color: dodgerblue'>未支付</span>";
                    }
                );
                $grid->smiple_type("收入/支出")->display(
                    function () {
                        if (\App\Models\Fund::getSimpleType($this->type) === \App\Models\Fund::TYPE_INCOME) {
                            return "<span style='color: forestgreen'>收入</span>";
                        }
                        if (\App\Models\Fund::getSimpleType($this->type) === \App\Models\Fund::TYPE_EXPENSES) {
                            return "<span style='color: orangered;'>支出</span>";
                        }

                        return "未知类型";
                    }
                );
                $grid->type('收入/支出类型')->display(
                    function ($value) {
                        return \App\Models\Fund::$typeMap[$value];
                    }
                );
                $grid->amount;
                $grid->description;
                $grid->create_admin_id("记账创建人")->display(
                    function ($value) {
                        return DB::table('admin_users')->where('id', $value)->first()->username;
                    }
                );
                $grid->update_admin_id("记账更新人")->display(
                    function ($value) {
                        return DB::table('admin_users')->where('id', $value)->first()->username;
                    }
                );
                $grid->created_at;
                $grid->updated_at->sortable();

                // 禁止按钮
                $grid->disableViewButton();
                $grid->disableDeleteButton();
                $grid->disableEditButton();
                $grid->disableBatchDelete();

                // 确认支付按钮
                $grid->actions(new FundPaid());

                $grid->selector(
                    function (Grid\Tools\Selector $selector) {
                        // 是否支付 规格筛选器
                        $selector->selectOne(
                            'pay_at',
                            '是否支付',
                            [
                                '已支付',
                                '未支付',
                                '所有',
                            ],
                            function ($query, $value) {
                                if ($value[0] == 1) {
                                    $query->whereNull('pay_at');
                                } else {
                                    if ($value[0] == 0) {
                                        $query->whereNotNull('pay_at');
                                    }
                                }
                            }
                        );

                        // 收入/支出 规格筛选器
                        $selector->selectOne(
                            'smiple_type',
                            '收入/支出',
                            [
                                '支出',
                                '收入',
                                '所有',
                            ],
                            function ($query, $value) {
                                if ($value[0] == 1) {
                                    $arr = \App\Models\Fund::getIncomeType();
                                    $query->whereIn('type', $arr);
                                } else {
                                    if ($value[0] == 0) {
                                        $arr = \App\Models\Fund::getExpensesType();
                                        $query->whereIn('type', $arr);
                                    }
                                }
                            }
                        );

                        // 收入/支出类型 规格筛选器
                        $selector->selectOne(
                            'type',
                            '收入/支出类型',
                            \App\Models\Fund::$typeMap,
                            function ($query, $value) {
                                if ($value[0] == 1) {
                                    $arr = \App\Models\Fund::getIncomeType();
                                    $query->whereIn('type', $arr);
                                } else {
                                    if ($value[0] == 0) {
                                        $arr = \App\Models\Fund::getExpensesType();
                                        $query->whereIn('type', $arr);
                                    }
                                }
                            }
                        );

                        $selector->selectOne(
                            'value',
                            '资金',
                            ["0-2999", "3000-5999", "6000-9999", "9999-99999"],
                            function ($query, $value) {
                                $between = [
                                    [0, 2999],
                                    [3000, 5999],
                                    [6000, 9999],
                                    [9999, 99999],
                                ];

                                $query->whereBetween('amount', $between[$value]);
                            }
                        );

                    }
                );

                $grid->filter(
                    function (Grid\Filter $filter) {
                        $filter->equal('id');
                    }
                );
            }
        );
    }

    protected function form()
    {
        return Form::make(
            new Fund(),
            function (Form $form) {
                $form->display('id');
                $form->select("type", "支出")->options(\App\Models\Fund::$incomeMap)->required()->rules('required');
                $form->decimal('amount')->required()->rules('required');
                $form->select('user_id', '选择收款人')
                    ->options(User::query()->where('type', User::TYPE_LANDLORD)->get()->pluck('name', 'id'))
                    ->required();
                $form->hidden("create_admin_id")->value(Auth::guard("admin")->id());
                $form->hidden("update_admin_id")->value(Auth::guard("admin")->id());

                $form->saving(
                    function (Form $form) {
                        $amount = $form->input("amount");
                        if ($amount <= 0) {
                            return $form->error("资金不能小于等于0");
                        }
                    }
                );
            }
        );
    }
}
